/* @flow */

import { isUndef } from '../../../../util/index'

let target
function updateDOMListeners(oldVnode, vnode) {
  if (isUndef(oldVnode.data.on) && isUndef(vnode.data.on)) {
    return
  }
  const on = vnode.data.on || {}
  target = vnode.elm
  let name,  cur
  for (name in on) {
    cur = on[name]
    if (isUndef(cur)) {
       console.error('事件名:'+String(cur)+' 不存在')
    }else{
      if(name === 'input'){ //v-model绑定的事件
        if(vnode.tag === 'select'){ 
          target.onchange=cur
        }else if(vnode.tag === 'textarea'){
          target['on'+ name] = cur
        }else if(vnode.tag === 'input'){
          const  isInput  = parseInputType(vnode.data.attrs && vnode.data.attrs.type)
          if(isInput){
            target['on'+ name] = cur
          }
        }
      }else{
        target['on'+ name] = cur
      }
    } 
  }
  target = undefined
}

export function parseInputType(type){
  const typeAttr = ['button','checkbox','file','hidden','image','radio','reset','submit']
  if(typeAttr.indexOf(type) > -1){
    return false
  }else {
    return true
  }
}
export default {
  create: updateDOMListeners,
  update: updateDOMListeners
}
